<?php
/**
 * Permission Service
 * User: zng
 * Date: 2019/11/4
 * Time: 14:34
 */
namespace app\admin\service\curriculum;

use app\admin\model\curriculum\{
    Teacher as TeacherModels
};

Class Teacher {

    /**
     * 首页列表
     * @param array $get 筛选条件
     * @author zx
     * @date 2019-12-06
     * @return \think\Paginator
     */
    public function indexPaginate( array $get=[] )
    {
        $map = [];
        if ( get($get, 'name') ) {
            $map[] = ['name', 'LIKE', $get['name'].'%'];
        }
        $pageSize = get($get,'pageSize')>0?$get['pageSize']:20;
        return TeacherModels::where($map)
            ->order('id', 'desc')
            ->paginate($pageSize);
    }

    /**
     * 新增教师信息
     * @param array $data 数据包
     * @return boolean|integer
     */
    public function create(array $data)
    {
        $teacher = TeacherModels::create($data, ['name','head_url','seniority','profiles']);
        return $teacher->isEmpty()?false:$teacher->id;
    }

    /**
     * 批量删除
     * @param mixed $data
     * @return boolean
     */
    public function destory( $data )
    {
        return TeacherModels::destroy(function($query)use($data){
            $where = ['id', '=', $data];
            if ( is_array($data) ) {
                $where = ['id', 'in', $data];
            }
            $query->where([
                $where
            ]);
        });
    }

    /**
     * 教师管理编辑
     * @param TeacherModels $model
     * @param array $data
     */
    public function save(TeacherModels $model, array $data)
    {
        $model->name = $data['name'];
        $model->head_url = $data['head_url'];
        $model->profiles = $data['profiles'];
        $model->seniority = $data['seniority'];
        return $model->save();
    }

    /**
     * 获取教师集合
     * @return array
     */
    public function getList()
    {
        return TeacherModels::field('id,name')->select()->toArray();
    }

}